'use strict';

angular.module('Help').controller('APITestController', ['$scope', '$resource', '$stateParams', 'PublicFunction', '$location', 'Authentication',
    function ($scope, $resource, $stateParams, PublicFunction, $location, Authentication) {
        $scope.Parameter = {
            User: null,
            Terminal: null,
            WorkLocation: null,
            Work: null,
            BarcodeMaster: null,
            BizRecord: null,
            QCRecords: null
        };
        $scope.Terminal = {
            UniqueID: '00:11:22:33'
        };
        $scope.btnGetWork = function () {
            var req = {
                Parameter: {
                    TerminalUniqueID: $scope.Terminal.UniqueID
                }
            };

            $resource('/api/FirstQC/getTerminalWorkLocationWorks').save(
                req,
                function (res) {
                    $scope.Parameter.Terminal = res.Data.Terminal;
                    $scope.Parameter.Work = res.Data.Works[0];
                    $scope.Parameter.WorkLocation = res.Data.WorkLocation;
                    PublicFunction.alert("查询终端、作业、工位成功", "查询成功");
                },
                function (err) {
                    PublicFunction.alert(err.message, err.title);
                }
            );
        };

        $scope.WorkLocation = null;
        $scope.Works = [];
        $scope.User = {
            UniqueID: '00000013'
        };
        $scope.btnGetUser = function () {
            var req = {
                Parameter: {
                    Barcode: $scope.User.UniqueID
                }
            };

            $resource('/api/FirstQC/getFirstQCDefaultInfo').save(
                req,
                function (res) {
                    $scope.Parameter.User = res.Data.User;
                    PublicFunction.alert("查询用户成功", "查询成功");
                },
                function (err) {
                    PublicFunction.alert(err.message, err.title);
                }
            );
        };

        $scope.BarcodeMaster = {
            Barcode: 'Item00000001'
        }
        $scope.WorkID = '56e22a2c7e9db62025e6b291';
        $scope.Data = null;
        $scope.btnGetItemMaster = function () {
            var req = {
                Parameter: {
                    Barcode: $scope.BarcodeMaster.Barcode,
                    WorkID: $scope.WorkID
                }
            };

            $resource('/api/FirstQC/getFirstQCDefaultInfo').save(
                req,
                function (res) {
                    $scope.Parameter.BarcodeMaster = res.Data.BarcodeMaster;
                    $scope.Parameter.BizRecord = res.Data.BizRecord;
                    $scope.Parameter.QCRecords = res.Data.QCRecords;
                    PublicFunction.alert("查询条码主档、流水、品质记录成功", "查询成功");
                },
                function (err) {
                    PublicFunction.alert(err.message, err.title);
                }
            );
        };


        $scope.setFirstQCInfo = function () {
            for (var i = 0; i < $scope.Parameter.QCRecords.length; i++) {
                $scope.Parameter.QCRecords[i].NormValue = i;
            }
            var req = {
                Parameter: $scope.Parameter
            };

            $resource('/api/FirstQC/setFirstQCInfo').save(
                req,
                function (res) {
                    PublicFunction.alert(res.Data);
                },
                function (err) {
                    PublicFunction.alert(err.message, err.title);
                }
            );
        };

        $scope.BadType = {
            ItemMasterId: '5666ae7c15e27c8024048600',
            WorkId: '56680a5fc71dd83c2af8aedf'
        };
        $scope.btnGetBadType = function () {
            var req = {
                Parameter: {
                    ItemMasterId: $scope.BadType.ItemMasterId,
                    WorkId: $scope.BadType.WorkId
                }
            };
            $resource('/api/FirstQC/getBadType').save(
                req,
                function (res) {
                    PublicFunction.alert("获取缺陷类型信息成功", "获取成功");
                },
                function (err) {
                    PublicFunction.alert(err.message, err.title);
                }
            );
        };

        $scope.Process = {
            Barcode: 'Item00000001',
            UniqueID: 'T0000002',
            BusinessStatus: false
        };
        $scope.onProcess = function () {
            var req = {
                Parameter: {
                    Barcode: $scope.Process.Barcode,
                    TerminalUniqueID: $scope.Process.UniqueID,
                    BusinessStatus: $scope.Process.BusinessStatus ? 1 : 0
                }
            };
            $resource('/api/Process/processScan').save(
                req,
                function (res) {
                    PublicFunction.alert("加工扫描成功", "扫描成功");
                },
                function (err) {
                    PublicFunction.alert(err.message, err.title);
                }
            );
        };

        $scope.Wrapper = {
            Barcode: 'Item00000001',
            UniqueID: '11:33:55:77'
        };
        var wrapperInfo;
        $scope.getWrapperInfo = function () {
            var req = {
                Parameter: {
                    Barcode: $scope.Wrapper.Barcode,
                    TerminalUniqueID: $scope.Wrapper.UniqueID
                }
            };
            $resource('/api/Wrapper/getWrapperInfo').save(
                req,
                function (res) {
                    wrapperInfo = res.Data;
                    PublicFunction.alert("获取卷制信息成功", "获取成功");
                },
                function (err) {
                    PublicFunction.alert(err.message, err.title);
                }
            );
        };
        $scope.setWrapperMeasure = function () {
            var req = {
                Parameter: {
                    Barcode: $scope.Wrapper.Barcode,
                    TerminalUniqueID: $scope.Wrapper.UniqueID,
                    Work: wrapperInfo.basicInfo.Work,
                    WorkName: wrapperInfo.basicInfo.WorkName,
                    Users: [],
                    QCRecords: []
                }
            };
            angular.forEach(wrapperInfo.basicInfo.Users, function (user) {
                req.Parameter.Users.push(user._id);
            });
            angular.forEach(wrapperInfo.qcStandard, function (qcStandard) {
                req.Parameter.QCRecords.push({
                    QCNorm: qcStandard.QCNorm,
                    QCStandard: qcStandard.QCStandard,
                    NormValue: 6
                });
            });
            $resource('/api/Wrapper/setWrapperMeasure').save(
                req,
                function (res) {
                    PublicFunction.alert("确认卷制测量信息成功", "确认成功");
                },
                function (err) {
                    PublicFunction.alert(err.message, err.title);
                }
            );
        };

        $scope.Judge = {
            PersionBarcode: '00000013',
            MasterBarcode: 'Item00000001',
            terminalUniqueID: 'TC0000001',
            QCStatus: 1,
            Remark: '特裁备注信息'
        };
        var judgeInfo = {};
        $scope.specialScanPersionCode = function () {
            var req = {
                Parameter: {
                    Barcode: $scope.Judge.PersionBarcode
                }
            };
            $resource('/api/Judge/specialScanCode').save(
                req,
                function (res) {
                    judgeInfo.userID = res._id;
                    PublicFunction.alert("获取人信息成功", "扫码成功");
                },
                function (err) {
                    PublicFunction.alert(err.message, err.title);
                }
            );
        };
        $scope.specialScanMasterCode = function () {
            var req = {
                Parameter: {
                    Barcode: $scope.Judge.MasterBarcode
                }
            };
            $resource('/api/Judge/specialScanCode').save(
                req,
                function (res) {
                    PublicFunction.alert("获取物信息成功", "扫码成功");
                },
                function (err) {
                    PublicFunction.alert(err.message, err.title);
                }
            );
        };
        $scope.doJudge = function () {
            var req = {
                Parameter: {
                    Barcode: $scope.Judge.MasterBarcode,
                    TerminalUniqueID: $scope.Judge.terminalUniqueID,
                    UserID: judgeInfo.userID,
                    QCStatus: $scope.Judge.QCStatus,
                    Remark: $scope.Judge.Remark
                }
            };
            $resource('/api/Judge/doJudge').save(
                req,
                function (res) {
                    PublicFunction.alert("执行特裁成功", "特裁成功");
                },
                function (err) {
                    PublicFunction.alert(err.message, err.title);
                }
            );
        };
        $scope.getBadMasters = function () {
            var req = {};
            $resource('/api/Judge/getBadMasters').save(
                req,
                function (res) {
                    PublicFunction.alert("获取不合格物料成功", "获取成功");
                },
                function (err) {
                    PublicFunction.alert(err.message, err.title);
                }
            );
        };

        $scope.ProductFlow = {
            Barcode: 'Item00000001'
        };
        $scope.getProductFlow = function () {
            var req = {
                Parameter: {
                    Barcode: $scope.ProductFlow.Barcode
                }
            };
            $resource('/api/ProductFlow/getProductFlow').save(
                req,
                function (res) {
                    PublicFunction.alert("获取单品流水成功", "获取成功");
                },
                function (err) {
                    PublicFunction.alert(err.message, err.title);
                }
            );
        };

        $scope.BarcodeMaster = {
            MOLotNumber: ''
        };
        $scope.getBarcodeMaster = function () {
            var req = {
                Parameter: {
                    MOLotNumber: $scope.BarcodeMaster.MOLotNumber
                }
            };
            $resource('/api/core/findAllBarcodeMasterByLotNum').save(
                req,
                function (res) {
                    PublicFunction.alert("获取BarcodeMaster成功", "获取成功");
                },
                function (err) {
                    PublicFunction.alert(err.message, err.title);
                }
            );
        };

        $scope.Kanban = {
            Barcode: 'Item00000001'
        };
        $scope.createKanban = function () {
            var req = {
                Parameter: {
                    Barcode: $scope.Kanban.Barcode
                }
            };
            $resource('/api/Kanban/createKanban').save(
                req,
                function (res) {
                    PublicFunction.alert("生成看板需求成功", "生成成功");
                },
                function (err) {
                    PublicFunction.alert(err.message, err.title);
                }
            );
        };

        $scope.user = {
            id: '56837f003da5ae88146c6d53'
        };
        $scope.searchUpKanban = function () {
            var req = {
                Parameter: {
                    userId: $scope.user.id
                }
            };
            $resource('/api/Kanban/GetUpStreamKanban').save(
                req,
                function (res) {
                    PublicFunction.alert("获取上游看板需求成功", "生成成功");
                },
                function (err) {
                    PublicFunction.alert(err.message, err.title);
                }
            );
        };
        $scope.searchDownKanban = function () {
            var req = {
                Parameter: {
                    userId: $scope.user.id
                }
            };
            $resource('/api/Kanban/GetDownStreamKanban').save(
                req,
                function (res) {
                    PublicFunction.alert("获取下游看板需求成功", "生成成功");
                },
                function (err) {
                    PublicFunction.alert(err.message, err.title);
                }
            );
        };

        $scope.udpatePIckUpQty = function () {
            var req = {
                Parameter: {
                    userId: "56837f003da5ae88146c6d53",
                    validBarcodeList: ['6901234567892', '69012341', '089600124569', '896000'],
                    invalidBarcodeList: ['1234567890']
                }
            };
            $resource('/api/Kanban/UdpatePIckUpQty').save(
                req,
                function (res) {
                    PublicFunction.alert("获取下游看板需求成功", "生成成功");
                },
                function (err) {
                    PublicFunction.alert(err.message, err.title);
                }
            );
        };
    }
]);
